/*******************************************************************************
 * @note      Copyright (C) 2018 Shanghai Panchip Microelectronics Co., Ltd.
 *            All rights reserved.
 *
 * @file      lwheap_port.h
 * @brief     
 *
 * @history   V0.01, 2018-11-07, xiaoguolin, first implementation.
*******************************************************************************/
#ifndef __LWHEAP_PORT_H
#define __LWHEAP_PORT_H

#ifdef __cplusplus
extern "C"{
#endif //__cplusplus

#include "lwheap.h"
#include "stdio.h"

#define __STATIC_INLINE static inline

extern LwHeap_t __heap;

typedef enum{
    LW_ALIGN_4 = 2,
    LW_ALIGN_8 = 3,
    LW_ALIGN_16 = 4,
    LW_ALIGN_32 = 5,
    LW_ALIGN_64 = 6,
    LW_ALIGN_128 = 7,
    LW_ALIGN_256 = 8,
    LW_ALIGN_512 = 9,
    LW_ALIGN_1024 = 10,
    LW_ALIGN_2048 = 11,
    LW_ALIGN_4096 = 12
}lw_align_t;

void lwheap_initial(void);

__STATIC_INLINE void* lw_malloc_align(size_t xsize, lw_align_t align)
{
    void* ptr = lwheap_malloc(&__heap, xsize, align);
    if(!ptr){
        printf("malloc failed\r\n");
    }
    return ptr;
}

__STATIC_INLINE void* lw_calloc_align(size_t n, size_t xsize, lw_align_t align)
{
    void* ptr = lwheap_malloc(&__heap, n * xsize, align);
    if(!ptr){
        printf("malloc failed\r\n");
    }
    return ptr;
}

__STATIC_INLINE void* lw_malloc(size_t xsize)
{
    return lw_malloc_align(xsize, LW_ALIGN_4);
}

__STATIC_INLINE void* lw_calloc(size_t n, size_t xsize){
    return lw_calloc_align(n, xsize, LW_ALIGN_4);
}

__STATIC_INLINE void lw_free(void *ptr)
{
    lwheap_free(&__heap, ptr);
}

#ifdef __cplusplus
}
#endif //__cplusplus

#endif //__LWHEAP_PORT_H
